<?php
/*
 * 手机客户端用户-登录接口
 * 2014-06-03
 */
 
include "../inc/public_header.php";
include "../func/user.php";				//用户信息处理函数

//常量
$mobile 	= $_REQUEST["mobile"];		
$type 		= $_REQUEST["type"];	

//手机发送短信验证码
$rs=array("success"=>"0");
if (isset($mobile) and is_numeric($mobile)){
	if (strlen($mobile) == 11){
		//短信平台设置
		srand((double)microtime()*1000000); 
		while(($vcode=rand()%100000)<10000);
		switch ($type){
			case 0:
				$content = "感谢您在中国老虎注册，验证码为".$vcode."。客服热线：400-108-6488（工作日9-18点）";
				break;
			case 1:
				$content = "您于".date('y-m-d h:i:s',time())."申请重置密码，验证码为".$vcode."。客服热线400-180-6488（工作日9-18点）";
				break;
		}
		//判断手机号是否注册
		//临时取消1
		$sql = "select uid,uuid from user_base where mobile = '".$mobile."'";
		$db->query($sql);
		if($db->next_record()){
			switch ($type){
				case 0:
					//该手机号已经被注册
					$rs=array("success"=>"0","errCode"=>"1");
					break;
				case 1:
					sendMessage($vcode,$mobile,$content);
					$sql1 = "replace into user_userpwdvcode(mobile,vcode,cuts) value('".$mobile."','".$vcode."',UNIX_TIMESTAMP())";
					$db1->query($sql1);
					$sql1 = "insert into user_smslog(mobile,senddt,sendobj,content) values('".$mobile."',now(),'".$type."','".$content."')";
					$db1->query($sql1);
					//用户密码重置
					$rs=array("success"=>"1");
					break;
			}
		}else{
			
			switch ($type){
				case 0:
					sendMessage($vcode,$mobile,$content);
					$sql1 = "replace into user_regvcode(mobile,vcode,cuts) value('".$mobile."','".$vcode."',UNIX_TIMESTAMP())";
					$db1->query($sql1);
					$sql1 = "insert into user_smslog(mobile,senddt,sendobj,content) values('".$mobile."',now(),'".$type."','".$content."')";
					$db1->query($sql1);
					//用户密码重置
					$rs=array("success"=>"1");
					break;
				case 1:
					//没有注册过不能重置密码
					$rs=array("success"=>"0","errCode"=>"1");
					break;
			}
		}
	}else{
		//目前只支持国内的手机号码
		$rs=array("success"=>"0","errCode"=>"-1");
	}
}else{
	//请正确填写手机号码
	$rs=array("success"=>"0","errCode"=>"-1");
}

echo json_encode($rs);

function sendMessage($authnum,$mobile, $message) 
{ 
	/* 通过企信通的HTTP接口*/
	$ip = "121.52.220.246";
	$port = "8888";
	$x_url = "http://sh.ipyy.com:8888/sms.aspx";
	$xx = "action=send&userid=1564&account=jkcs45&password=jkcs45888&mobile=" . $mobile . "&content=" . $message . "";
    

	
	$fp = fsockopen($ip, $port, $errno, $errstr, 10);
	$reply = "";
	if ($fp) {
		fputs($fp, "POST " . $x_url . " HTTP/1.1\r\n");
		fputs($fp, "Host: " . $ip . "\r\n");
		fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
		fputs($fp, "Content-length: " . strlen($xx) . "\r\n");
		fputs($fp, "Connection: close\r\n\r\n");
		fputs($fp, $xx . "\r\n\r\n");

		$reply = "";
		while (!feof($fp)) {
			$reply .= fgets($fp, 4096);
		}
	}
	$reply_char=explode("Connection: close",$reply);
	$result=explode("-",trim($reply_char[1]));
	if(trim($result[0])=="DELIVRD"){
		return "00";
	}else{
		return "01";
	}

}
?>
